package com.cqust.service;

import com.cqust.mapper.AdminMapper;
import com.cqust.mapper.EmployeeMapper;
import com.cqust.pojo.Admin;
import com.cqust.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    public boolean login(String eid, String password) {
        List<Admin> admins = adminMapper.getAdmins();
        for (Admin admin : admins) {
            if(eid.equals(admin.getEid())&&password.equals(admin.getPassword())){
                return true;
            }
        }
        return false;
    }

    public boolean register(Admin admin, String passwordTwo) {
        String name = admin.getName();
        String eid = admin.getEid();
        String password = admin.getPassword();
        if(name.equals("")||password.equals("")||eid.equals("")) return false;
        if(!password.equals(passwordTwo)) return false;
        List<Employee> employees = employeeMapper.getEmployees();
        for (Employee employee : employees) {
            if(employee.getId().equals(eid)) {
                adminMapper.addAdmin(admin);
                return true;
            };
        }
        return false;
    }

    public Admin getAdminByEid(String eid) {
        return adminMapper.getAdminByEid(eid);
    }
}
